package com.eveinfo.display.planner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import java.util.Map.Entry;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.eveinfo.R;
import com.eveinfo.api.character.sheet.ApiSheet;
import com.eveinfo.api.utils.certificates.ApiCertificate;
import com.eveinfo.api.utils.skilllist.ApiSkill;
import com.eveinfo.tools.Tools;

public class PlannerTools {

	public static Dialog createDialogAddSkill(final Context context, final PlannerManager plannerManager,
			ApiSheet sheet, final ApiSkill skill) {
		final Dialog dialog;
		dialog = new Dialog(context);
		dialog.setContentView(R.layout.addplannerskill);
		dialog.setTitle("Plan skill:");
		String[] itemsLevel = Tools.getTrainnableLevel(sheet, skill);
		if (itemsLevel == null) {
			return null;
		}

		final Spinner spinnerLevel = (Spinner) dialog.findViewById(R.id.SAddSkillLevel);
		ArrayAdapter<String> adapterLevel = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item,
				itemsLevel);
		adapterLevel.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerLevel.setAdapter(adapterLevel);

		ArrayList<String> plannersName = plannerManager.getPlannersName();
		int size = plannersName.size();
		if (size == 0) {
			// TODO
			return null;
		}
		String[] itemsPlanner = new String[size];
		itemsPlanner = plannersName.toArray(itemsPlanner);

		final Spinner spinnerPlanner = (Spinner) dialog.findViewById(R.id.SAddSkillPlanner);
		ArrayAdapter<String> adapterPlanner = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item,
				itemsPlanner);
		adapterPlanner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerPlanner.setAdapter(adapterPlanner);

		Button submit = (Button) dialog.findViewById(R.id.BAddSkill);
		submit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String selectedItem = (String) spinnerLevel.getSelectedItem();
				char charAt = selectedItem.charAt(6);
				int level = Character.getNumericValue(charAt);

				String plannerName = (String) spinnerPlanner.getSelectedItem();

				plannerManager.addSkillToPlanner(plannerName, skill, level);
				dialog.dismiss();
			}
		});
		return dialog;
	}

	public static Dialog createDialogAddCertificate(final Context context, final PlannerManager plannerManager,
			final ApiCertificate certificate) {
		final Dialog dialog;
		dialog = new Dialog(context);
		dialog.setContentView(R.layout.addplannercertificate);
		dialog.setTitle("Plan certificate:");

		ArrayList<String> plannersName = plannerManager.getPlannersName();
		int size = plannersName.size();
		if (size == 0) {
			// TODO
			return null;
		}
		String[] itemsPlanner = new String[size];
		itemsPlanner = plannersName.toArray(itemsPlanner);

		final Spinner spinnerPlanner = (Spinner) dialog.findViewById(R.id.SAddCertificatePlanner);
		ArrayAdapter<String> adapterPlanner = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item,
				itemsPlanner);
		adapterPlanner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerPlanner.setAdapter(adapterPlanner);

		Button submit = (Button) dialog.findViewById(R.id.BAddCertificate);
		submit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String plannerName = (String) spinnerPlanner.getSelectedItem();
				HashMap<Integer, Integer> requiredSkills = certificate.getRequiredSkills();
				Set<Entry<Integer, Integer>> entrySet = requiredSkills.entrySet();
				for (Entry<Integer, Integer> entry : entrySet) {
					plannerManager.addSkillToPlanner(plannerName, entry.getKey(), entry.getValue());
				}
				dialog.dismiss();
			}
		});
		return dialog;
	}

	public static Dialog createDialogAddSkill(final Context context, final PlannerManager plannerManager,
			final ApiSkill skill, final int level) {
		final Dialog dialog;
		dialog = new Dialog(context);
		dialog.setContentView(R.layout.addplannercertificate);
		dialog.setTitle("Plan skill:");

		if(plannerManager == null){
			return null;
		}
		ArrayList<String> plannersName = plannerManager.getPlannersName();
		int size = plannersName.size();
		if (size == 0) {
			// TODO
			return null;
		}
		String[] itemsPlanner = new String[size];
		itemsPlanner = plannersName.toArray(itemsPlanner);

		final Spinner spinnerPlanner = (Spinner) dialog.findViewById(R.id.SAddCertificatePlanner);
		ArrayAdapter<String> adapterPlanner = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item,
				itemsPlanner);
		adapterPlanner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerPlanner.setAdapter(adapterPlanner);

		Button submit = (Button) dialog.findViewById(R.id.BAddCertificate);
		submit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String plannerName = (String) spinnerPlanner.getSelectedItem();
				plannerManager.addSkillToPlanner(plannerName, skill, level);
				dialog.dismiss();
			}
		});
		return dialog;
	}
}
